package com.shangfutai.android.keymonitoringf3

import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.PowerManager
import android.view.WindowManager
import androidx.annotation.RequiresApi
import android.annotation.SuppressLint
import android.app.WallpaperManager




class ScreenActivity : AppCompatActivity() {

    var wakeLock:PowerManager.WakeLock?=null

    @SuppressLint("MissingPermission")
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        val window = getWindow()
        window.addFlags(
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                or WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)


        val wallPaper = WallpaperManager.getInstance(this).drawable
        window.setBackgroundDrawable(wallPaper)

        setContentView(R.layout.activity_screen)

        if (Build.VERSION.SDK_INT >= 26) {
            KeyMonitoringService.start(this)
        }else{
            startForegroundService(Intent(this,KeyMonitoringService::class.java))
        }
    }

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
//        wakeLock.let {
//            val pm =  getSystemService(Context.POWER_SERVICE) as PowerManager
//            wakeLock = pm.newWakeLock(
//                PowerManager.ACQUIRE_CAUSES_WAKEUP
//                    or PowerManager.SCREEN_BRIGHT_WAKE_LOCK, MainActivity::class.java.name)
//        }
//        wakeLock?.acquire()
//        if (Build.VERSION.SDK_INT >= 26) {
//            KeyMonitoringService.start(this)
//        }else{
//            startForegroundService(Intent(this,MainActivity::class.java))
//        }
//        wakeLock?.release()

    }

    override fun onDestroy() {
        super.onDestroy()
        wakeLock?.release()
    }
}